package com.czk.springcloud.service.impl;

import com.czk.springcloud.dao.OrderDao;
import com.czk.springcloud.domain.Order;
import com.czk.springcloud.service.AccountService;
import com.czk.springcloud.service.OrderService;
import com.czk.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/12/27 14:45
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     * GlobalTransactional开启全局异常控制
     *
     * @param order
     */
    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        // 1创建订单
        log.info("开始创建订单");
        orderDao.create(order);
        // 2扣减库存
        log.info("订单微服务考试调用库存，做扣减开始");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("订单微服务考试调用库存，做扣减结束");
        // 3扣减账户
        log.info("订单微服务考试调用账户，做扣减开始");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("订单微服务考试调用账户，做扣减结束");
        // 修改订单状态
        log.info("修改订单状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("修改订单状态结束");
        log.info("下订单成功结束了");
    }
}
